{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "892d69b4",
   "metadata": {},
   "source": [
    "# 数值类型（number）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af6cbe49",
   "metadata": {},
   "source": [
    "* 整型（int）  \n",
    "* 浮点型（float、double）  \n",
    "* 复数（基本不用）  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "003adce7",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0a8556ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f5573d0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = 10.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "72aea475",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.5"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2dcaf221",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3850e962",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fe74a053",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以改变type类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7e217a3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6405220d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "929c27bf",
   "metadata": {},
   "source": [
    "## 内置函数计算函数\n",
    "* 绝对值abs（）\n",
    "* min（）最小值\n",
    "* max（）最大值\n",
    "* sun（）总值 sum的参数数量有限，仅限两个，但是可以以上的任意多个  可以放个列表[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1b9acb85",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = -100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1f759cd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-100"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "10f9e7d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abs(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "83798b36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(1,2,3,4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "538f0d6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(1,3,6,8,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "869aabee",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "sum() takes at most 2 arguments (4 given)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [21]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;43msum\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: sum() takes at most 2 arguments (4 given)"
     ]
    }
   ],
   "source": [
    "sum(1,3,4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "68f3b9d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([1,3,4,5])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b86d97fb",
   "metadata": {},
   "source": [
    "-----  \n",
    "##  内置模块\n",
    "* math\n",
    "* random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6f851ca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "85323398",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__doc__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__spec__',\n",
       " 'acos',\n",
       " 'acosh',\n",
       " 'asin',\n",
       " 'asinh',\n",
       " 'atan',\n",
       " 'atan2',\n",
       " 'atanh',\n",
       " 'ceil',\n",
       " 'comb',\n",
       " 'copysign',\n",
       " 'cos',\n",
       " 'cosh',\n",
       " 'degrees',\n",
       " 'dist',\n",
       " 'e',\n",
       " 'erf',\n",
       " 'erfc',\n",
       " 'exp',\n",
       " 'expm1',\n",
       " 'fabs',\n",
       " 'factorial',\n",
       " 'floor',\n",
       " 'fmod',\n",
       " 'frexp',\n",
       " 'fsum',\n",
       " 'gamma',\n",
       " 'gcd',\n",
       " 'hypot',\n",
       " 'inf',\n",
       " 'isclose',\n",
       " 'isfinite',\n",
       " 'isinf',\n",
       " 'isnan',\n",
       " 'isqrt',\n",
       " 'lcm',\n",
       " 'ldexp',\n",
       " 'lgamma',\n",
       " 'log',\n",
       " 'log10',\n",
       " 'log1p',\n",
       " 'log2',\n",
       " 'modf',\n",
       " 'nan',\n",
       " 'nextafter',\n",
       " 'perm',\n",
       " 'pi',\n",
       " 'pow',\n",
       " 'prod',\n",
       " 'radians',\n",
       " 'remainder',\n",
       " 'sin',\n",
       " 'sinh',\n",
       " 'sqrt',\n",
       " 'tan',\n",
       " 'tanh',\n",
       " 'tau',\n",
       " 'trunc',\n",
       " 'ulp']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(math)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45fd3e7e",
   "metadata": {},
   "source": [
    "#### random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "72b63e13",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "48ab6c61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['BPF',\n",
       " 'LOG4',\n",
       " 'NV_MAGICCONST',\n",
       " 'RECIP_BPF',\n",
       " 'Random',\n",
       " 'SG_MAGICCONST',\n",
       " 'SystemRandom',\n",
       " 'TWOPI',\n",
       " '_Sequence',\n",
       " '_Set',\n",
       " '__all__',\n",
       " '__builtins__',\n",
       " '__cached__',\n",
       " '__doc__',\n",
       " '__file__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__spec__',\n",
       " '_accumulate',\n",
       " '_acos',\n",
       " '_bisect',\n",
       " '_ceil',\n",
       " '_cos',\n",
       " '_e',\n",
       " '_exp',\n",
       " '_floor',\n",
       " '_inst',\n",
       " '_log',\n",
       " '_os',\n",
       " '_pi',\n",
       " '_random',\n",
       " '_repeat',\n",
       " '_sha512',\n",
       " '_sin',\n",
       " '_sqrt',\n",
       " '_test',\n",
       " '_test_generator',\n",
       " '_urandom',\n",
       " '_warn',\n",
       " 'betavariate',\n",
       " 'choice',\n",
       " 'choices',\n",
       " 'expovariate',\n",
       " 'gammavariate',\n",
       " 'gauss',\n",
       " 'getrandbits',\n",
       " 'getstate',\n",
       " 'lognormvariate',\n",
       " 'normalvariate',\n",
       " 'paretovariate',\n",
       " 'randbytes',\n",
       " 'randint',\n",
       " 'random',\n",
       " 'randrange',\n",
       " 'sample',\n",
       " 'seed',\n",
       " 'setstate',\n",
       " 'shuffle',\n",
       " 'triangular',\n",
       " 'uniform',\n",
       " 'vonmisesvariate',\n",
       " 'weibullvariate']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(random)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "314cdeec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.choice([1,3,4,5,6,7,8]) # 随机选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6099252e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.choice([1,3,4,5,6,7,8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d9216d8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle([1,2,3,4,5,6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9e2f23a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3,4,5,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3eaaebc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "195ca12f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 4, 5, 3, 2, 1]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "4efd15a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ff375eff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 4, 1, 2, 3]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "aadc5aa0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 4, 1, 2, 3]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "7bfe9f61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6506981864007001"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.random()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b2ad352",
   "metadata": {},
   "source": [
    "# 元组类型（tuple）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "802b2c0f",
   "metadata": {},
   "source": [
    "* scores = （23，45，12，85）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "424f719d",
   "metadata": {},
   "source": [
    "## tuple(元组 重数)的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "54fdc6ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = (1,4,8,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "6f45bd22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6412918d",
   "metadata": {},
   "source": [
    "## tuple的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0615a8b6",
   "metadata": {},
   "source": [
    "### 创建\n",
    "* 1.a = ()\n",
    "* 2.a = tuple()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4354f77f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = (\"a\",\"b\",\"c\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "316c1567",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = ()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "243c26b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = tuple()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "48e5d62b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "d879ac3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = (3,4,6,8,9) # 这里有5个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "3a43ebe8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2] # 这里访问第三个元素  在编程里面，第一个元素的索引是从0开始的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "378c0485",
   "metadata": {},
   "source": [
    "### a[ ] 访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "020cd223",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[-1] # 从右往左数 -1 -2......"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e70f69a",
   "metadata": {},
   "source": [
    "### a[1:3] 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "ea186a21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 6)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:3] # 1是正常能取的，但是3不可以（？）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "35865fa4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[-1:-3] # 从右到左没有，不形成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "7af54925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 8)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[-3:-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "e60e76fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4, 6, 8, 9)"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "20088051",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [10]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m a[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "a[0] = 1 # 将第一个元素重新定义为1 但是tuple不可更改元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "7b8ab77f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4, 6, 8, 9)"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "f9476e75",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [72]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m a\n",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "del a # a 被删除，没有定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "47fc5c50",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [73]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1bc82bf",
   "metadata": {},
   "source": [
    "### tuple 相关方法\n",
    "* len() 计算长度\n",
    "* count() 出现次数\n",
    "* index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "5986080d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = (1,3,4,5,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "fdadf243",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "9d394a67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.count(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "b0e95389",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.index(3) # a值里面的3的下表是什么"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "064e4264",
   "metadata": {},
   "source": [
    "***\n",
    "# 列表类型（list）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1d9a192",
   "metadata": {},
   "source": [
    "* scores = [89,56,12,45]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87c32d80",
   "metadata": {},
   "source": [
    "### list的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7c819400",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,3,5,6,4,] # 用方括号表示[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "344a76a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "3f536b4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = list() #用关键词表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "864e7f7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a) # 也可以创建一个空的list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "392e8187",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "19828cb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,3,5,6,4,]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "81973c6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 6, 4]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "a647d8a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = (1,3,5,6,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "85bed73e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 3, 5, 6, 4)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "f7311296",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "8aa5d610",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [93]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m b[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "b[2] = 10 # tuple第二个元素是3，但是b[2] = 10 重新定义第二个元素，但tuple不允许重新定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99d73392",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "332eae6e",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "a[2] = 10 # 在tuple中元素是不能被重新定义的，但是list中元素可以被重新定义 相比上边的a的第三个元素，发生变化,原本a = [1,3,4,5,4],变换成a = [1,3,10,6,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "edee3622",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 10, 6, 4]"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15795469",
   "metadata": {},
   "source": [
    "----\n",
    "### list的基本操作\n",
    "* 切片 a[:]\n",
    "* 访问元素 a[]\n",
    "* 增加元素1.append()→在尾部追加一个数  2.insert（）→可以在某个元素里面插入一个元素\n",
    "* 删除元素  \n",
    "       1.remove() 删除里面其中一个，如果有重复，就删最前面的  \n",
    "       2.pop()删除-1，就是最后一个值  \n",
    "       3. clear() 全部清除\n",
    "* 更改元素 a[2] =22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "5a91c618",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 10, 6, 4]"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "b581ea51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1]"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:1] # 切片 只能取到0的元素，1的元素取不到"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "1a16d165",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 10, 6, 4]"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "f1a01bdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.append(7) # append:追加，在尾部追加一个7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "deeeac1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 10, 6, 4, 7, 7]"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "690f2f66",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.insert(1,1000) # 在元素0和1之间插入一个1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "5b05a094",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1000, 3, 10, 6, 4, 7, 7]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9526c03",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b0817a6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,3,4,5,3,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8feaa31f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.remove(3)  #删除一个指定的值 a.remove(值),如果有重复，只删除前面的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7cc02f6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 5, 3, 6]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7ffe4c4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.pop() #pop 将最后一个值弹出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "964003a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 5, 3]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a #再次查看a时，已经将最后一个值删除了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d42c4a38",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.clear() # 这是将a的值全部清除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d08a9826",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fe6dcba1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on list object:\n",
      "\n",
      "class list(object)\n",
      " |  list(iterable=(), /)\n",
      " |  \n",
      " |  Built-in mutable sequence.\n",
      " |  \n",
      " |  If no argument is given, the constructor creates a new empty list.\n",
      " |  The argument must be an iterable if specified.\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __delitem__(self, key, /)\n",
      " |      Delete self[key].\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(...)\n",
      " |      x.__getitem__(y) <==> x[y]\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __iadd__(self, value, /)\n",
      " |      Implement self+=value.\n",
      " |  \n",
      " |  __imul__(self, value, /)\n",
      " |      Implement self*=value.\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __reversed__(self, /)\n",
      " |      Return a reverse iterator over the list.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  __setitem__(self, key, value, /)\n",
      " |      Set self[key] to value.\n",
      " |  \n",
      " |  __sizeof__(self, /)\n",
      " |      Return the size of the list in memory, in bytes.\n",
      " |  \n",
      " |  append(self, object, /)\n",
      " |      Append object to the end of the list.\n",
      " |  \n",
      " |  clear(self, /)\n",
      " |      Remove all items from list.\n",
      " |  \n",
      " |  copy(self, /)\n",
      " |      Return a shallow copy of the list.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  extend(self, iterable, /)\n",
      " |      Extend list by appending elements from the iterable.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  insert(self, index, object, /)\n",
      " |      Insert object before index.\n",
      " |  \n",
      " |  pop(self, index=-1, /)\n",
      " |      Remove and return item at index (default last).\n",
      " |      \n",
      " |      Raises IndexError if list is empty or index is out of range.\n",
      " |  \n",
      " |  remove(self, value, /)\n",
      " |      Remove first occurrence of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  reverse(self, /)\n",
      " |      Reverse *IN PLACE*.\n",
      " |  \n",
      " |  sort(self, /, *, key=None, reverse=False)\n",
      " |      Sort the list in ascending order and return None.\n",
      " |      \n",
      " |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      " |      order of two equal elements is maintained).\n",
      " |      \n",
      " |      If a key function is given, apply it once to each list item and sort them,\n",
      " |      ascending or descending, according to their function values.\n",
      " |      \n",
      " |      The reverse flag can be set to sort in descending order.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __hash__ = None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a)  #这是list全部的相关方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c573f900",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,4,5,6,10,8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4da2f6fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.reverse() #取反"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "69871743",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 5, 6, 10, 8]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "baf0f704",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 5, 6, 8, 10]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sort()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "cf291e7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.sort)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48dbcabd",
   "metadata": {},
   "source": [
    "* name_and_age = {\n",
    "    \"zhangsan\":23,\n",
    "    \"lisi\":24,\n",
    "    \"wangwu\":12\n",
    "}  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bee42d52",
   "metadata": {},
   "source": [
    "# 集合类型（set）\n",
    "> set的创建  \n",
    ">> 1.用花括号表示：a = {}  \n",
    ">> 2.用关键词表示：a = set()  \n",
    "\n",
    "> set的基本操作  \n",
    ">> 增加元素  1.add()   2.update()  \n",
    ">> 删除元素  1.pop()   2.remove()   3.discard()   4.clear()  \n",
    ">> 集合操作  1.交集——intersection()   2.并集——unione()   3.差集——difference()  \n",
    "\n",
    "> set的相关方法  \n",
    ">> isdisjoint()  \n",
    ">> issubset()  \n",
    ">> issuperset()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45940e6e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08341aa3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5a6051a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4d47af1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0de5720",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "125724e9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93f6129c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc93fbd5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "606a576d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c5f9b0e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e44a3dd3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "650b5c34",
   "metadata": {},
   "source": [
    "***\n",
    "# 字符串str类型\n",
    "> str的定义：\n",
    ">> 用单或双引号表示  message = \"hello world\"    \n",
    ">> 用关键词表示  message = str(\"str\")  \n",
    "\n",
    "> str的基本操作：    \n",
    ">> 访问字符——message[6]  \n",
    ">> 切片——message[2:6]  \n",
    ">> 字符串长len(message)  \n",
    ">> 字符串拼接 1.“hello”+“world”   2.“hello”*3  \n",
    ">>判断是否存在子字符串 1.\"hi\" in \"hello\"  2.\"h\" not in \"hello\"  \n",
    ">>字符串格式化  \n",
    "\n",
    "> 相关方法  \n",
    ">> format()  \n",
    ">> upper()将字母大写  \n",
    ">> lower()  \n",
    ">> split()  \n",
    ">> join()  \n",
    ">> endswith()  \n",
    ">> replace()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "201caaa1",
   "metadata": {},
   "outputs": [],
   "source": [
    "message = \"hello world\" #可以用双引号也可以用单引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fab82e9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1782d9c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "message = str(\"hello world\") #第二种表达方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c2ee89d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "392c304e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5776c38e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message[1] #访问字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fc2123ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ll'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message[2:4] #为什么这个切片可以取不到4这个索引值呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "04497c5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(message) #这个字符串的长度包括了中间的空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2dae190b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'helloworld'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello'+'world' #字符串拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bc963740",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hellohellohello'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello'*3 #hello重复三次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7f86162e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'h' in \"hello\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "58d6b9c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"q\" in \"hello\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "67fc77b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "message = \"nihao\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "09bf561d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'mrt' in message #也可以多个查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2ec48874",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'m' 'i' in message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ffcd0b21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello, xuzhichao'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello, {}\".format(\"xuzhichao\") # {} 表示空格  用format替代位置 {}是一个占位符，将format里面的内容替换到{}里  字符串的格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e7ecfd26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class str in module builtins:\n",
      "\n",
      "class str(object)\n",
      " |  str(object='') -> str\n",
      " |  str(bytes_or_buffer[, encoding[, errors]]) -> str\n",
      " |  \n",
      " |  Create a new string object from the given object. If encoding or\n",
      " |  errors is specified, then the object must expose a data buffer\n",
      " |  that will be decoded using the given encoding and error handler.\n",
      " |  Otherwise, returns the result of object.__str__() (if defined)\n",
      " |  or repr(object).\n",
      " |  encoding defaults to sys.getdefaultencoding().\n",
      " |  errors defaults to 'strict'.\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __format__(self, format_spec, /)\n",
      " |      Return a formatted version of the string as described by format_spec.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(...)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mod__(self, value, /)\n",
      " |      Return self%value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __rmod__(self, value, /)\n",
      " |      Return value%self.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  __sizeof__(self, /)\n",
      " |      Return the size of the string in memory, in bytes.\n",
      " |  \n",
      " |  __str__(self, /)\n",
      " |      Return str(self).\n",
      " |  \n",
      " |  capitalize(self, /)\n",
      " |      Return a capitalized version of the string.\n",
      " |      \n",
      " |      More specifically, make the first character have upper case and the rest lower\n",
      " |      case.\n",
      " |  \n",
      " |  casefold(self, /)\n",
      " |      Return a version of the string suitable for caseless comparisons.\n",
      " |  \n",
      " |  center(self, width, fillchar=' ', /)\n",
      " |      Return a centered string of length width.\n",
      " |      \n",
      " |      Padding is done using the specified fill character (default is a space).\n",
      " |  \n",
      " |  count(...)\n",
      " |      S.count(sub[, start[, end]]) -> int\n",
      " |      \n",
      " |      Return the number of non-overlapping occurrences of substring sub in\n",
      " |      string S[start:end].  Optional arguments start and end are\n",
      " |      interpreted as in slice notation.\n",
      " |  \n",
      " |  encode(self, /, encoding='utf-8', errors='strict')\n",
      " |      Encode the string using the codec registered for encoding.\n",
      " |      \n",
      " |      encoding\n",
      " |        The encoding in which to encode the string.\n",
      " |      errors\n",
      " |        The error handling scheme to use for encoding errors.\n",
      " |        The default is 'strict' meaning that encoding errors raise a\n",
      " |        UnicodeEncodeError.  Other possible values are 'ignore', 'replace' and\n",
      " |        'xmlcharrefreplace' as well as any other name registered with\n",
      " |        codecs.register_error that can handle UnicodeEncodeErrors.\n",
      " |  \n",
      " |  endswith(...)\n",
      " |      S.endswith(suffix[, start[, end]]) -> bool\n",
      " |      \n",
      " |      Return True if S ends with the specified suffix, False otherwise.\n",
      " |      With optional start, test S beginning at that position.\n",
      " |      With optional end, stop comparing S at that position.\n",
      " |      suffix can also be a tuple of strings to try.\n",
      " |  \n",
      " |  expandtabs(self, /, tabsize=8)\n",
      " |      Return a copy where all tab characters are expanded using spaces.\n",
      " |      \n",
      " |      If tabsize is not given, a tab size of 8 characters is assumed.\n",
      " |  \n",
      " |  find(...)\n",
      " |      S.find(sub[, start[, end]]) -> int\n",
      " |      \n",
      " |      Return the lowest index in S where substring sub is found,\n",
      " |      such that sub is contained within S[start:end].  Optional\n",
      " |      arguments start and end are interpreted as in slice notation.\n",
      " |      \n",
      " |      Return -1 on failure.\n",
      " |  \n",
      " |  format(...)\n",
      " |      S.format(*args, **kwargs) -> str\n",
      " |      \n",
      " |      Return a formatted version of S, using substitutions from args and kwargs.\n",
      " |      The substitutions are identified by braces ('{' and '}').\n",
      " |  \n",
      " |  format_map(...)\n",
      " |      S.format_map(mapping) -> str\n",
      " |      \n",
      " |      Return a formatted version of S, using substitutions from mapping.\n",
      " |      The substitutions are identified by braces ('{' and '}').\n",
      " |  \n",
      " |  index(...)\n",
      " |      S.index(sub[, start[, end]]) -> int\n",
      " |      \n",
      " |      Return the lowest index in S where substring sub is found,\n",
      " |      such that sub is contained within S[start:end].  Optional\n",
      " |      arguments start and end are interpreted as in slice notation.\n",
      " |      \n",
      " |      Raises ValueError when the substring is not found.\n",
      " |  \n",
      " |  isalnum(self, /)\n",
      " |      Return True if the string is an alpha-numeric string, False otherwise.\n",
      " |      \n",
      " |      A string is alpha-numeric if all characters in the string are alpha-numeric and\n",
      " |      there is at least one character in the string.\n",
      " |  \n",
      " |  isalpha(self, /)\n",
      " |      Return True if the string is an alphabetic string, False otherwise.\n",
      " |      \n",
      " |      A string is alphabetic if all characters in the string are alphabetic and there\n",
      " |      is at least one character in the string.\n",
      " |  \n",
      " |  isascii(self, /)\n",
      " |      Return True if all characters in the string are ASCII, False otherwise.\n",
      " |      \n",
      " |      ASCII characters have code points in the range U+0000-U+007F.\n",
      " |      Empty string is ASCII too.\n",
      " |  \n",
      " |  isdecimal(self, /)\n",
      " |      Return True if the string is a decimal string, False otherwise.\n",
      " |      \n",
      " |      A string is a decimal string if all characters in the string are decimal and\n",
      " |      there is at least one character in the string.\n",
      " |  \n",
      " |  isdigit(self, /)\n",
      " |      Return True if the string is a digit string, False otherwise.\n",
      " |      \n",
      " |      A string is a digit string if all characters in the string are digits and there\n",
      " |      is at least one character in the string.\n",
      " |  \n",
      " |  isidentifier(self, /)\n",
      " |      Return True if the string is a valid Python identifier, False otherwise.\n",
      " |      \n",
      " |      Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n",
      " |      such as \"def\" or \"class\".\n",
      " |  \n",
      " |  islower(self, /)\n",
      " |      Return True if the string is a lowercase string, False otherwise.\n",
      " |      \n",
      " |      A string is lowercase if all cased characters in the string are lowercase and\n",
      " |      there is at least one cased character in the string.\n",
      " |  \n",
      " |  isnumeric(self, /)\n",
      " |      Return True if the string is a numeric string, False otherwise.\n",
      " |      \n",
      " |      A string is numeric if all characters in the string are numeric and there is at\n",
      " |      least one character in the string.\n",
      " |  \n",
      " |  isprintable(self, /)\n",
      " |      Return True if the string is printable, False otherwise.\n",
      " |      \n",
      " |      A string is printable if all of its characters are considered printable in\n",
      " |      repr() or if it is empty.\n",
      " |  \n",
      " |  isspace(self, /)\n",
      " |      Return True if the string is a whitespace string, False otherwise.\n",
      " |      \n",
      " |      A string is whitespace if all characters in the string are whitespace and there\n",
      " |      is at least one character in the string.\n",
      " |  \n",
      " |  istitle(self, /)\n",
      " |      Return True if the string is a title-cased string, False otherwise.\n",
      " |      \n",
      " |      In a title-cased string, upper- and title-case characters may only\n",
      " |      follow uncased characters and lowercase characters only cased ones.\n",
      " |  \n",
      " |  isupper(self, /)\n",
      " |      Return True if the string is an uppercase string, False otherwise.\n",
      " |      \n",
      " |      A string is uppercase if all cased characters in the string are uppercase and\n",
      " |      there is at least one cased character in the string.\n",
      " |  \n",
      " |  join(self, iterable, /)\n",
      " |      Concatenate any number of strings.\n",
      " |      \n",
      " |      The string whose method is called is inserted in between each given string.\n",
      " |      The result is returned as a new string.\n",
      " |      \n",
      " |      Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'\n",
      " |  \n",
      " |  ljust(self, width, fillchar=' ', /)\n",
      " |      Return a left-justified string of length width.\n",
      " |      \n",
      " |      Padding is done using the specified fill character (default is a space).\n",
      " |  \n",
      " |  lower(self, /)\n",
      " |      Return a copy of the string converted to lowercase.\n",
      " |  \n",
      " |  lstrip(self, chars=None, /)\n",
      " |      Return a copy of the string with leading whitespace removed.\n",
      " |      \n",
      " |      If chars is given and not None, remove characters in chars instead.\n",
      " |  \n",
      " |  partition(self, sep, /)\n",
      " |      Partition the string into three parts using the given separator.\n",
      " |      \n",
      " |      This will search for the separator in the string.  If the separator is found,\n",
      " |      returns a 3-tuple containing the part before the separator, the separator\n",
      " |      itself, and the part after it.\n",
      " |      \n",
      " |      If the separator is not found, returns a 3-tuple containing the original string\n",
      " |      and two empty strings.\n",
      " |  \n",
      " |  removeprefix(self, prefix, /)\n",
      " |      Return a str with the given prefix string removed if present.\n",
      " |      \n",
      " |      If the string starts with the prefix string, return string[len(prefix):].\n",
      " |      Otherwise, return a copy of the original string.\n",
      " |  \n",
      " |  removesuffix(self, suffix, /)\n",
      " |      Return a str with the given suffix string removed if present.\n",
      " |      \n",
      " |      If the string ends with the suffix string and that suffix is not empty,\n",
      " |      return string[:-len(suffix)]. Otherwise, return a copy of the original\n",
      " |      string.\n",
      " |  \n",
      " |  replace(self, old, new, count=-1, /)\n",
      " |      Return a copy with all occurrences of substring old replaced by new.\n",
      " |      \n",
      " |        count\n",
      " |          Maximum number of occurrences to replace.\n",
      " |          -1 (the default value) means replace all occurrences.\n",
      " |      \n",
      " |      If the optional argument count is given, only the first count occurrences are\n",
      " |      replaced.\n",
      " |  \n",
      " |  rfind(...)\n",
      " |      S.rfind(sub[, start[, end]]) -> int\n",
      " |      \n",
      " |      Return the highest index in S where substring sub is found,\n",
      " |      such that sub is contained within S[start:end].  Optional\n",
      " |      arguments start and end are interpreted as in slice notation.\n",
      " |      \n",
      " |      Return -1 on failure.\n",
      " |  \n",
      " |  rindex(...)\n",
      " |      S.rindex(sub[, start[, end]]) -> int\n",
      " |      \n",
      " |      Return the highest index in S where substring sub is found,\n",
      " |      such that sub is contained within S[start:end].  Optional\n",
      " |      arguments start and end are interpreted as in slice notation.\n",
      " |      \n",
      " |      Raises ValueError when the substring is not found.\n",
      " |  \n",
      " |  rjust(self, width, fillchar=' ', /)\n",
      " |      Return a right-justified string of length width.\n",
      " |      \n",
      " |      Padding is done using the specified fill character (default is a space).\n",
      " |  \n",
      " |  rpartition(self, sep, /)\n",
      " |      Partition the string into three parts using the given separator.\n",
      " |      \n",
      " |      This will search for the separator in the string, starting at the end. If\n",
      " |      the separator is found, returns a 3-tuple containing the part before the\n",
      " |      separator, the separator itself, and the part after it.\n",
      " |      \n",
      " |      If the separator is not found, returns a 3-tuple containing two empty strings\n",
      " |      and the original string.\n",
      " |  \n",
      " |  rsplit(self, /, sep=None, maxsplit=-1)\n",
      " |      Return a list of the words in the string, using sep as the delimiter string.\n",
      " |      \n",
      " |        sep\n",
      " |          The delimiter according which to split the string.\n",
      " |          None (the default value) means split according to any whitespace,\n",
      " |          and discard empty strings from the result.\n",
      " |        maxsplit\n",
      " |          Maximum number of splits to do.\n",
      " |          -1 (the default value) means no limit.\n",
      " |      \n",
      " |      Splits are done starting at the end of the string and working to the front.\n",
      " |  \n",
      " |  rstrip(self, chars=None, /)\n",
      " |      Return a copy of the string with trailing whitespace removed.\n",
      " |      \n",
      " |      If chars is given and not None, remove characters in chars instead.\n",
      " |  \n",
      " |  split(self, /, sep=None, maxsplit=-1)\n",
      " |      Return a list of the words in the string, using sep as the delimiter string.\n",
      " |      \n",
      " |      sep\n",
      " |        The delimiter according which to split the string.\n",
      " |        None (the default value) means split according to any whitespace,\n",
      " |        and discard empty strings from the result.\n",
      " |      maxsplit\n",
      " |        Maximum number of splits to do.\n",
      " |        -1 (the default value) means no limit.\n",
      " |  \n",
      " |  splitlines(self, /, keepends=False)\n",
      " |      Return a list of the lines in the string, breaking at line boundaries.\n",
      " |      \n",
      " |      Line breaks are not included in the resulting list unless keepends is given and\n",
      " |      true.\n",
      " |  \n",
      " |  startswith(...)\n",
      " |      S.startswith(prefix[, start[, end]]) -> bool\n",
      " |      \n",
      " |      Return True if S starts with the specified prefix, False otherwise.\n",
      " |      With optional start, test S beginning at that position.\n",
      " |      With optional end, stop comparing S at that position.\n",
      " |      prefix can also be a tuple of strings to try.\n",
      " |  \n",
      " |  strip(self, chars=None, /)\n",
      " |      Return a copy of the string with leading and trailing whitespace removed.\n",
      " |      \n",
      " |      If chars is given and not None, remove characters in chars instead.\n",
      " |  \n",
      " |  swapcase(self, /)\n",
      " |      Convert uppercase characters to lowercase and lowercase characters to uppercase.\n",
      " |  \n",
      " |  title(self, /)\n",
      " |      Return a version of the string where each word is titlecased.\n",
      " |      \n",
      " |      More specifically, words start with uppercased characters and all remaining\n",
      " |      cased characters have lower case.\n",
      " |  \n",
      " |  translate(self, table, /)\n",
      " |      Replace each character in the string using the given translation table.\n",
      " |      \n",
      " |        table\n",
      " |          Translation table, which must be a mapping of Unicode ordinals to\n",
      " |          Unicode ordinals, strings, or None.\n",
      " |      \n",
      " |      The table must implement lookup/indexing via __getitem__, for instance a\n",
      " |      dictionary or list.  If this operation raises LookupError, the character is\n",
      " |      left untouched.  Characters mapped to None are deleted.\n",
      " |  \n",
      " |  upper(self, /)\n",
      " |      Return a copy of the string converted to uppercase.\n",
      " |  \n",
      " |  zfill(self, width, /)\n",
      " |      Pad a numeric string with zeros on the left, to fill a field of the given width.\n",
      " |      \n",
      " |      The string is never truncated.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  maketrans(...)\n",
      " |      Return a translation table usable for str.translate().\n",
      " |      \n",
      " |      If there is only one argument, it must be a dictionary mapping Unicode\n",
      " |      ordinals (integers) or characters to Unicode ordinals, strings or None.\n",
      " |      Character keys will be then converted to ordinals.\n",
      " |      If there are two arguments, they must be strings of equal length, and\n",
      " |      in the resulting dictionary, each character in x will be mapped to the\n",
      " |      character at the same position in y. If there is a third argument, it\n",
      " |      must be a string, whose characters will be mapped to None in the result.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8764eaa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "message = \"ni hao\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "2395b5d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ni hao'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2cdd30be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ni', 'hao']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message.split() # 将空格分割，切割完成后，变成两个元素  一个ni 一个hao 而[] 是list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e2970e33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hello;world']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello;world'.split() # 这个字符串没有被空格隔开，切割的就只有一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "4987056b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hello', 'world']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello;world'.split(\";\") #如果还是要切割开来的话，就在split里面指定在哪里切割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "fbef3096",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hello;', 'orld']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello;world'.split(\"w\") #？？？为什么这个直接将w删去了呢"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "1b27c20a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello.world'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\".\".join(['hello','world']) #()里面是列表，放了两个元素 join后将一个列表里面的两个元素变成一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "62c77444",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'helloworld'.endswith('d') #这是字符串是不是以d结束  结束endswith 开始startswith"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "26381d83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'nihaoworld'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'helloworld'.replace('hello','nihao') # replace里面，先放一个要替代的参数，然后 ， 然后再放替换参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57ef74e0",
   "metadata": {},
   "source": [
    "# 字典（dict）\n",
    "> dict的创建  \n",
    ">> 1.用花括号{}表示： a = {}  \n",
    ">> 2.用关键词表示： a = dict（）  \n",
    "\n",
    "> dict的基本操作   \n",
    ">> 1.访问元素   1. a[key]   2.a.get(key)    \n",
    ">> 2.增加元素  \n",
    ">> 3.删除元素    1. del   2. pop()   3.popitem()    4.clear()   \n",
    ">> 4.更改元素——update()  \n",
    "\n",
    "> dict的相关方法 \n",
    ">> items()  \n",
    ">> values()  \n",
    ">> keys()   \n",
    ">> sorted()  \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "86ba3b0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {} # 一个空内容的dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "04e7773a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9eb4ba25",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {\"name\":\"zhangsan\",\"gendar\":\"m\"} # {}里面是字符串，字符串要用\"\"或者''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9df7003b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bbb77672",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangsan', 'gendar': 'm'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "589a7a57",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = dict(name=\"zhangsan\",gender=\"m\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b9e7a8f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangsan', 'gender': 'm'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "96266ec0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhangsan'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[\"name\"] #访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "166ed3d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'m'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[\"gender\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "51ee2627",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'age'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [16]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mage\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'age'"
     ]
    }
   ],
   "source": [
    "a[\"age\"] #  用这种方式访问一个不存在的元素时，会显示关键词错误，即不存在这个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0c654f8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.get(\"age\") #当用a.get(\"key\")的形式访问元素时，如果元素不存在，并不会报错，只是不会显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0390e9c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhangsan'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.get(\"name\") #第二种访问元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8a8c77f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[\"age\"] = 20 #增加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b7485377",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangsan', 'gender': 'm', 'age': 20}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "dd3b85d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangsan', 'gender': 'm', 'age': 20, 'address': 'beijing'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.update(address=\"beijing\") # update 更改元素，此代码更改为a中增加address\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f21c47d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "del a [\"age\"] # 删除元素 删除a中的age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0f2e02ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangsan', 'address': 'beijing'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5b4cfab1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhangsan'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.pop(\"name\") # pop一个元素，然后就会弹出元素的值  ##刚刚运行了一次，已经将gender给pop掉了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "11023950",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'address': 'beijing'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "45ac0605",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('address', 'beijing')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.popitem() # 其实popitem是随机将a里面的元素和值提取出来然后进行删除，但是前面我已经删到只有一个address了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "fb1c8249",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a # 所以现在a只是一个空元素了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "dac05f88",
   "metadata": {},
   "outputs": [],
   "source": [
    "# a.clear()是将a全部清除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "563ea895",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class dict in module builtins:\n",
      "\n",
      "class dict(object)\n",
      " |  dict() -> new empty dictionary\n",
      " |  dict(mapping) -> new dictionary initialized from a mapping object's\n",
      " |      (key, value) pairs\n",
      " |  dict(iterable) -> new dictionary initialized as if via:\n",
      " |      d = {}\n",
      " |      for k, v in iterable:\n",
      " |          d[k] = v\n",
      " |  dict(**kwargs) -> new dictionary initialized with the name=value pairs\n",
      " |      in the keyword argument list.  For example:  dict(one=1, two=2)\n",
      " |  \n",
      " |  Built-in subclasses:\n",
      " |      StgDict\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      True if the dictionary has the specified key, else False.\n",
      " |  \n",
      " |  __delitem__(self, key, /)\n",
      " |      Delete self[key].\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(...)\n",
      " |      x.__getitem__(y) <==> x[y]\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __ior__(self, value, /)\n",
      " |      Return self|=value.\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __or__(self, value, /)\n",
      " |      Return self|value.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __reversed__(self, /)\n",
      " |      Return a reverse iterator over the dict keys.\n",
      " |  \n",
      " |  __ror__(self, value, /)\n",
      " |      Return value|self.\n",
      " |  \n",
      " |  __setitem__(self, key, value, /)\n",
      " |      Set self[key] to value.\n",
      " |  \n",
      " |  __sizeof__(...)\n",
      " |      D.__sizeof__() -> size of D in memory, in bytes\n",
      " |  \n",
      " |  clear(...)\n",
      " |      D.clear() -> None.  Remove all items from D.\n",
      " |  \n",
      " |  copy(...)\n",
      " |      D.copy() -> a shallow copy of D\n",
      " |  \n",
      " |  get(self, key, default=None, /)\n",
      " |      Return the value for key if key is in the dictionary, else default.\n",
      " |  \n",
      " |  items(...)\n",
      " |      D.items() -> a set-like object providing a view on D's items\n",
      " |  \n",
      " |  keys(...)\n",
      " |      D.keys() -> a set-like object providing a view on D's keys\n",
      " |  \n",
      " |  pop(...)\n",
      " |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      " |      \n",
      " |      If key is not found, default is returned if given, otherwise KeyError is raised\n",
      " |  \n",
      " |  popitem(self, /)\n",
      " |      Remove and return a (key, value) pair as a 2-tuple.\n",
      " |      \n",
      " |      Pairs are returned in LIFO (last-in, first-out) order.\n",
      " |      Raises KeyError if the dict is empty.\n",
      " |  \n",
      " |  setdefault(self, key, default=None, /)\n",
      " |      Insert key with a value of default if key is not in the dictionary.\n",
      " |      \n",
      " |      Return the value for key if key is in the dictionary, else default.\n",
      " |  \n",
      " |  update(...)\n",
      " |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.\n",
      " |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]\n",
      " |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v\n",
      " |      In either case, this is followed by: for k in F:  D[k] = F[k]\n",
      " |  \n",
      " |  values(...)\n",
      " |      D.values() -> an object providing a view on D's values\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      " |  \n",
      " |  fromkeys(iterable, value=None, /) from builtins.type\n",
      " |      Create a new dictionary with keys from iterable and values set to value.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __hash__ = None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7cf8bacd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'zhangsan', 'age': '20', 'address': 'beijing'}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = dict(name=\"zhangsan\",age=\"20\",address=\"beijing\")\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "6be4b106",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('name', 'zhangsan'), ('age', '20'), ('address', 'beijing')])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.items() # 返回所有的key和values 并且以tuple的形式呈现出来 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "bd9d77ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "ad9e1b4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['name', 'age', 'address'])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e267079c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['zhangsan', '20', 'beijing'])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "ee9dd1ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['address', 'age', 'name']"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(a) # 注意排序的拼写方法 按26字母排序 并且返回一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9275e93f",
   "metadata": {},
   "outputs": [],
   "source": [
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
